package com.wn.ticket.web.backstage;

import com.wn.ticket.common.ResponseEntity;
import com.wn.ticket.entity.Coupon;
import com.wn.ticket.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@RequestMapping("/backstage/coupon")
public class CouponController {
    @Autowired
    private CouponService couponService;

    @PreAuthorize("hasAnyAuthority('super')")
    @RequestMapping("/add")
    public ResponseEntity add(@RequestBody Coupon coupon) {

        couponService.add(coupon);

        return ResponseEntity.success;
    }

    @RequestMapping("/getAll")
    public ResponseEntity getAll() {

        List<Coupon> couponList = couponService.getAll();

        return new ResponseEntity("200", "ok", couponList);
    }

    @RequestMapping("/getByCondition")
    public ResponseEntity getByCondition(@RequestBody Coupon coupon) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//获取系统时间
        String rt = df.format(System.currentTimeMillis());
        coupon.setCurrentTime(rt);
        List<Coupon> couponList = couponService.getByCondition(coupon);
        return new ResponseEntity("200", "ok", couponList);
    }


}
